package com.xtxk.utils.util;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.xtxk.common.constant.CommonConstant;
import com.xtxk.common.constant.QuantityConstant;
import com.xtxk.common.enums.AgreementTypeEnum;
import com.xtxk.common.enums.SettingInfoEnum;
import com.xtxk.utils.sqlite.SettingInfo;

/**
 * 公共方法
 */
@Component
public class CommonMethodUtils {

	private static final String HTTP_PREFIX = "http://";

	private static final String HTTPS_PREFIX = "https://";

	/**
	 * 获取运维终端配置信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static SettingInfo getSettingInfo(){
		return SettingInfoEnum.INSTANCE.getSettingInfo();
	}

	/**
	 * 根据settingInfo设置确定请求url路径
	 * 
	 * @param settingInfo
	 * @param methodName
	 * @return 例如:
	 *         https://172.16.3.51:8443/YWTerminal/V1/GetUnBindDownShareDeviceByCenterID
	 */
	public static String getRequestUrl(String methodName) {
		StringBuffer resultUlr = new StringBuffer();
		SettingInfo settingInfo = getSettingInfo();
		// 以http方式请求
		if (settingInfo.getAgreement_type().equals(AgreementTypeEnum.HTTP.getValue())) {
			resultUlr.append(HTTP_PREFIX).append(settingInfo.getDSU_IP()).append(":").append(settingInfo.getDSU_PORT())
					.append(CommonConstant.DSU_PRO_NAME).append("/").append(methodName);
		} else {
			resultUlr.append(HTTPS_PREFIX).append(settingInfo.getDSU_IP()).append(":").append(settingInfo.getDSU_PORT())
					.append(CommonConstant.DSU_PRO_NAME).append("/").append(methodName);
		}
		return resultUlr.toString();
	}

	/**
	 * 发送请求
	 * @param agreementType
	 * @param url
	 * @param params        请求参数
	 * @param requestMethod 请求方式 GET OR POST
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 */
	public static String sendRequest(String url, Map<String, Object> params, String requestMethod) {
		if (StringUtils.isBlank(requestMethod) || StringUtils.isBlank(url)) {
			return null;
		}
		// 进行http请求
		if (getSettingInfo().getAgreement_type().equals(AgreementTypeEnum.HTTP.getValue())) {
			

		} else {
			if (requestMethod.equals(QuantityConstant.DOGET)) {
				
				return HttpsUtil.doGet(url, params);
			} else {
				try {
					return HttpsUtil.doPost(url, params);
				} catch (KeyManagementException | NoSuchAlgorithmException | IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	/**
	 * 发送请求
	 * @param agreementType
	 * @param url
	 * @param params        请求参数
	 * @param requestMethod 请求方式 GET OR POST
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 */
	public String sendRequestBodyXml(Integer agreementType, String url, String params, String requestMethod) {
		if (StringUtils.isBlank(requestMethod) || StringUtils.isBlank(url) || agreementType == null) {
			return null;
		}
		if (agreementType.equals(AgreementTypeEnum.HTTP.getValue())) {

		} else {
			if (requestMethod.equals(QuantityConstant.DOPOST)) {
				try {
					return HttpsUtil.doPost(url, params);
				} catch (KeyManagementException | NoSuchAlgorithmException | IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	public static Map<String, Object> conver2Map(Object ob){
		if (Objects.isNull(ob)) {
			return null;
		}
		return JSONObject.parseObject(JSONObject.toJSONString(ob));
	}
}
